package net.towee.server.ftp.files;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import net.towee.model.repo.AttachmentIdentity;
import net.towee.model.repo.DocumentNode;
import net.towee.server.persistence.AttachmentStore;

import org.apache.ftpserver.ftplet.FtpFile;

/**
 * Attachment file
 * 
 * @author c58
 */
public class AttachmentFtpFile extends AbstractFtpFile {
	private final AttachmentStore attStore;
	private final AttachmentIdentity id;
	private final DocumentNode node;

	public AttachmentFtpFile(DocumentNode node, AttachmentIdentity id, AttachmentStore attStore) {
		super(id.getName(), false, id.getSize(), "", "", FtpFileType.ATTACHMENT, 
				"/"	+ node.getDocumentIdentity().getGroup().getAlias() + node.getPath() + "/"
						+ "attachments/" + id.getName());
		this.node = node;
		this.id = id;
		this.attStore = attStore;
	}

	@Override
	public long getLastModified() {
		return 0;
	}

	@Override
	public List<FtpFile> listFiles() {
		return null;
	}

	@Override
	public OutputStream createOutputStream(long offset) throws IOException {
		return null;
	}

	@Override
	public InputStream createInputStream(long offset) throws IOException {
		// Increment downloads for file
		attStore.incrementDownloads(id.getId());
		
		// Get streem
		InputStream is = attStore.getAttachment(id.getId()).getInputStream();
		
		// Skip offset bytes
		is.skip(offset);
		
		return is;
	}

}